import { componentCompileLogger } from "../logger/componentCompileLogger";
import * as vscode from 'vscode';
import * as path from 'path';
import { PushFileToDevicePanel } from "./pushFileToDevicePanel";

export function pushFileToDevice(args: any[], context: vscode.ExtensionContext) {
    const clickPath = args[0].path;

    const fs = require('fs');
    let targetPath = clickPath;
    if (fs.existsSync(clickPath)) {
        const stats = fs.statSync(clickPath);
        if (stats.isFile()) {
            // If it's a file, get its directory
            targetPath = path.dirname(clickPath);
            componentCompileLogger.info(`Path is a file, using directory: ${targetPath}`);
        } else {
            componentCompileLogger.info(`Path is a directory: ${targetPath}`);
        }
    } else {
        componentCompileLogger.errorWithStack(`Path does not exist: ${clickPath}`);
        return;
    }
    
    // Store the path for use in the panel
    context.workspaceState.update('pushFileToDevicePath', targetPath);


    PushFileToDevicePanel.createOrShow(context.extensionUri, context);

    return;
}